import React  from 'react';
import { observable, action,computed,toJS} from 'mobx';
import { NavLink} from 'react-router-dom';
import { Badge,Tools} from 'weaver-mobile';
import { ListViewPage } from 'weaver-mobile-page';
import * as  Apis from '../../apis/govern';
import {CommonText} from '../../components/governTools/comText/text';
import ActionSheetStore from '../agent/actionSheetStore.js';
import { SupStore } from "../supStore.js";
const getLabel = Tools.getLabel;
const commonText = new CommonText();
/*
 * @info: MainStore
 * @Author: lusx
 * @Date: 2019-12-01 11:13:16
 * @Last modified by:   lusx
 * @Last modified time: 13-03-2019 05:31:23
 */
export default class MainStore extends SupStore{
  @observable page = 0;//标签页Tabs组件的当前pag
  @observable selectKey = '0';//标签栏当前TabBar.Item
  PROTAB0 = getLabel(commonText.PROTAB0[0],commonText.PROTAB0[1]);
  PROTAB1 = getLabel(commonText.PROTAB1[0],commonText.PROTAB1[1]);
  PROTAB2 = getLabel(commonText.PROTAB2[0],commonText.PROTAB2[1]);
  PROTAB3 = getLabel(commonText.PROTAB3[0],commonText.PROTAB3[1]);
  TASKTAB0 = getLabel(commonText.TASKTAB0[0],commonText.TASKTAB0[1]);
  TASKTAB1 = getLabel(commonText.TASKTAB1[0],commonText.TASKTAB1[1]);
  TASKTAB2 = getLabel(commonText.TASKTAB2[0],commonText.TASKTAB2[1]);
  TASKTAB3 = getLabel(commonText.TASKTAB3[0],commonText.TASKTAB3[1]);
  proTab = [{text:0,to:'/pro/newduty',value:this.PROTAB0},//责任事项
              {text:0,to:'/pro/replace',value:this.PROTAB1},//代发事项
              {text:0,to:'/pro/monitor',value:this.PROTAB2},//监控列表
              {text:0,to:'/pro/matter',value:this.PROTAB3}];//查询结果
  taskTab = [{text:0,to:'/task/host',value:this.TASKTAB0},//主办
              {text:0,to:'/task/assist',value:this.TASKTAB1},//协办
              {text:0,to:'/task/finish',value:this.TASKTAB2},//归档
              {text:0,to:'/task/result',value:this.TASKTAB3}];//查询结果
  @computed get typeTab() {//获取标签页Tabs组件的按钮数据
        return this.selectKey === '0' ? {'tabArray':this.proTab} : {'tabArray':this.taskTab};
    }
  apisNameObj = {
                '0':{'fun':Apis.getProListResult,'name':'getProListResult'},
                '1':{'fun':Apis.getMyTaskList,'name':'getMyTaskList'},
                '2':{'fun':Apis.getAttentionTaskList,'name':'getAttentionTaskList'},
                '3':{'fun':Apis.getMyTaskList,'name':'getMyTaskList'}
              };//返回事项、任务、关注、提醒接口
  apisFun = function(){};//长列表接口函数
  @observable tabs = [];//标签页Tabs组件的tab数据
  getDataParams = [];//存储长列表接口入参
  cloneDataParams = [];//getDataParams的备份
  @observable listViewStore = new ListViewPage.Store({ route: '/pro/duty' });
  @observable doInitDatasSign = true;//执行长列表组件初始化方法标志
  @observable dataKey = {};
  @observable actionSheetStore = new ActionSheetStore(this);

  /**
   * 初始化长列表接口入参 ;获取当前页标签栏TabBar key值 ;设置标签页Tabs组件的tab数据 ；获取请求的接口函数
   * @method getSelectKey
   * @for MainStore
   * @param {参数类型} init 参数说明  底部tabBar对应的key值;
   * @return {返回值类型}
   */
  getSelectKey = (selectKey)=>{
    this.getDataParams = [//长列表接口入参
      {'dataType':'0'},//事项
      {'task':[{'dealtype':'0'},{'dealtype':'1'},{'listType':'finish'},{}]},//任务
      {},//我的关注
      {'mobileRemind':'1'}//即将到期
    ];
    this.selectKey = selectKey;
    this.getDataParams = this.getDataParams[parseInt(selectKey)];
    this.cloneDataParams = this.getDataParams;
    this.tabs = this.typeTab.tabArray.map((item,index)=>({
      title: (<Badge text={item.text}>
        {item.value}
      </Badge>)
    }//生成Tabs标签的tab数据
  ));
    this.apisFun = this.apisNameObj[this.selectKey];
    this.tabChange();
  }
  //标签页Tabs组件页签变化时触发,获取标签页Tabs组件的当前pag; 最终组装长列表接口入参;
  @action
  tabChange = (index=0)=>{
    this.page = index;
    const objKeyArray = Object.keys(this.cloneDataParams);
    if(this.cloneDataParams[objKeyArray[0]] instanceof Array && this.cloneDataParams[objKeyArray[0]].length>0){
      this.getDataParams = this.cloneDataParams[objKeyArray[0]][index];
    }else if (objKeyArray.length>0 && objKeyArray[0] === 'dataType') {
      this.getDataParams = {'dataType': `${index}`};
    }else {
      this.getDataParams = this.cloneDataParams;
    }
  }
/**
 * 各列表组件初始化方法：请求长列表接口；
 * @method initDatas
 * @for MainStore
 * @param {参数类型} {"path":String} 参数说明  type,路由path;
 * @return {返回值类型}
 */
  @action
  initDatas = (params={})=>{
    if(Object.keys(params).length > 1 ){//列表初始化加了参数path，所以改为1
      this.doInitDatasSign = false;
    }
  //  debugger
    const newParams = {
      ...this.getDataParams,
      "isSuper":"0",
      ...params
    }
    this.listViewStore = new ListViewPage.Store({ route: newParams.path });//19.1.22重新new ListViewPage.Store,解决tab页切换快，数据渲染不对，实质是给对应的route
    return  this.getData(newParams);
  }
  /**
   * 请求列表接口
   * @method getData
   * @for MainStore
   * @param {参数类型} {"type":String} 参数说明  type,路由path;
   * @return {返回值类型}
   */
  @action
  getData = (params)=>{
    this.loadingToast(true);
    return this.apisFun['fun'](params).then((data) => {
      this.loadingToast(false);
      this.dataKey = {
        'dataKey': data.sessionkey
      }
      this.listViewStore.getData(this.dataKey);
      //重置tab标签上的表头提醒
      if(this.apisFun.name === 'getProListResult' || this.apisFun.name === 'getMyTaskList'){
        if(params.doSearch){//搜索来的
          this.apisFun.name === 'getProListResult' && (this.proTab[this.page].text = data.unread);
          this.apisFun.name === 'getMyTaskList' && (this.taskTab[this.page].text = data.unread);
        }else{//正常切换tab页签来的
            data.unReadList.map((item,index)=>{
            this.apisFun.name === 'getProListResult' && (this.proTab[index].text = item);
            this.apisFun.name === 'getMyTaskList' && (this.taskTab[index].text = item);
          });
        }
        this.tabs = this.typeTab.tabArray.map((item,index)=>({
          title: (<Badge text={item.text}>
            {item.value}
          </Badge>)
        }//生成Tabs标签的tab数据
      ));
      }
    })
  }
  //清空
  @action
  clearState = (callback)=>{
    this.listViewStore.clearData();
    //this.listViewStore.setState();
    this.doInitDatasSign = true;
  }
}
